/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.faare.unittests.service.services.references;

import opiam.admin.faare.PropertiesManager;
import opiam.admin.faare.exception.ServiceException;
import opiam.admin.faare.service.UserContext;
import opiam.admin.faare.service.services.StandardService;
import opiam.admin.faare.service.services.references.ReferenceElement;
import opiam.admin.faare.service.services.references.ReferenceList;
import opiam.admin.faare.service.services.references.ReferenceService;
import opiam.admin.faare.unittests.TestResult;
import opiam.admin.faare.unittests.TestUtils;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import org.apache.log4j.Logger;

import java.util.List;


/**
 * This class allows to test the reference service.
 */
public class ReferenceServiceTest extends TestCase
{
    /** Instance of logger. */
    private static Logger _logger = Logger.getLogger(ReferenceServiceTest.class);

    /** Instance of the trace class. */
    private static TestUtils _test = new TestUtils(_logger);

    /** Test user DN. */
    private static final String DN_USER = "uid=jreuter, ou=People, dc=mycompany,dc=com";

    /** Test user login. */
    private static final String LOGIN_USER = "jreuter"; //uid

    /** Test user password. */
    private static final String PASSWORD_USER = "destroy";

    /** User context. */
    private static UserContext _USERCONTEXT = new UserContext();

    /**
     * Creates a new ReferenceServiceTest object.
     *
     * @param param  Parameter.
     */
    public ReferenceServiceTest(String param)
    {
        super(param);

        try
        {
            PropertiesManager.getInstance();

            if (!_USERCONTEXT.isLoggedIn())
            {
                StandardService.logon(LOGIN_USER, PASSWORD_USER, _USERCONTEXT);
            }
        }
        catch (Exception e)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("ReferenceServiceTest constructor error");
            msg.append(TestUtils.NEXT);
            msg.append(e.toString());
            _test.displayResultError(msg.toString());
        }
    }

    /**
     * Method of the tests suite.
     *
     * @return Tests result.
     */
    public static Test suite()
    {
        TestSuite suite = new TestSuite();

        suite.addTest(new ReferenceServiceTest("testBegin"));

        // TESTS SET
        suite.addTest(new ReferenceServiceTest("testGetReferenceListAppli"));
        suite.addTest(new ReferenceServiceTest("testGetReferenceListAppliParse"));
        suite.addTest(new ReferenceServiceTest("testGetReferenceListRequest"));
        suite.addTest(new ReferenceServiceTest("testGetReferenceListSession"));

        suite.addTest(new ReferenceServiceTest(
                "testGetSearchReferenceListAppli"));
        suite.addTest(new ReferenceServiceTest(
                "testGetSearchReferenceListAppliNoSort"));
        suite.addTest(new ReferenceServiceTest(
                "testGetSearchReferenceListRequest"));
        suite.addTest(new ReferenceServiceTest(
                "testGetSearchReferenceListSession"));

        suite.addTest(new ReferenceServiceTest("testEnd"));

        return (suite);
    }

    /**
     * Main method of the test class.
     *
     * @param args  Sets of the parameters.
     */
    public static void main(String[] args)
    {
        junit.textui.TestRunner.run(suite());
    }

    /**
     * Method to indicate the beginning of the class test.
     *
     */
    public static void testBegin()
    {
        //  To initialize the tests statement
        TestResult.getInstance().setInitiator("ReferenceServiceTest");

        _test.displayHeaderClass("ReferenceServiceTest");
        assertTrue(true);
    }

    /**
     * Method to indicate the end of the class test.
     *
     */
    public static void testEnd()
    {
        _test.displayFooterClass(null);
        assertTrue(true);

        // To display the tests statement
        _test.displayTestStatement("ReferenceServiceTest");
    }

    /**
     * Tests the Reference list with Application scope.
     */
    public void testGetReferenceListAppli()
    {
        _test.displayHeaderMethod("testGetReferenceListAppli");

        try
        {
            try
            {
                ReferenceList refList = ReferenceService.getReferenceList("obj_appli",
                        null);

                if (refList.getName().equals("obj_appli"))
                {
                    _test.displayMsg("Values list :");

                    for (int i = 0; i < refList.getReferenceList().size();
                            i++)
                    {
                        String refgroup = ((ReferenceElement) refList.getReferenceList()
                                                                     .get(i)).toString();
                        _test.displayMsg(refgroup);
                    }

                    if (refList.getReferenceList().size() == 4)
                    {
                        _test.displayResultSuccess(null);
                    }
                    else
                    {
                        StringBuffer fMsg = new StringBuffer();
                        fMsg.append("List size = ");
                        fMsg.append(refList.getReferenceList().size());
                        fMsg.append(" , expected 4.");
                        _test.displayResultFailure(fMsg.toString());
                    }
                }
            }
            catch (ServiceException srvex)
            {
                _test.displayResultFailure(srvex.toString());
            }
        }
        catch (Exception ex)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("Exception : ");
            msg.append(TestUtils.NEXT);
            msg.append(ex.toString());
            _test.displayResultError(msg.toString());
        }

        _test.displayFooterMethod("testGetReferenceListAppli");
    }

    /**
     * Tests the Reference list with Application scope.
     */
    public void testGetReferenceListAppliParse()
    {
        _test.displayHeaderMethod("testGetReferenceListAppliParse");

        try
        {
            try
            {
                ReferenceList refList = ReferenceService.getReferenceList("obj_appli_p",
                        null);

                if (refList.getName().equals("obj_appli_p"))
                {
                    _test.displayMsg("Values list :");

                    for (int i = 0; i < refList.getReferenceList().size();
                            i++)
                    {
                        String refgroup = ((ReferenceElement) refList.getReferenceList()
                                                                     .get(i)).toString();
                        _test.displayMsg(refgroup);
                    }

                    if (refList.getReferenceList().size() == 4)
                    {
                        _test.displayResultSuccess(null);
                    }
                    else
                    {
                        StringBuffer fMsg = new StringBuffer();
                        fMsg.append("List size = ");
                        fMsg.append(refList.getReferenceList().size());
                        fMsg.append(" , expected 4.");
                        _test.displayResultFailure(fMsg.toString());
                    }
                }
            }
            catch (ServiceException srvex)
            {
                _test.displayResultFailure(srvex.toString());
            }
        }
        catch (Exception ex)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("Exception : ");
            msg.append(TestUtils.NEXT);
            msg.append(ex.toString());
            _test.displayResultError(msg.toString());
        }

        _test.displayFooterMethod("testGetReferenceListAppliParse");
    }

    /**
     * Tests the Reference list with Request scope.
     */
    public void testGetReferenceListRequest()
    {
        _test.displayHeaderMethod("testGetReferenceListRequest");

        try
        {
            try
            {
                ReferenceList refList = ReferenceService.getReferenceList("obj_req",
                        _USERCONTEXT);

                if (refList.getName().equals("obj_req"))
                {
                    _test.displayMsg("Values list :");

                    for (int i = 0; i < refList.getReferenceList().size();
                            i++)
                    {
                        String refgroup = ((ReferenceElement) refList.getReferenceList()
                                                                     .get(i)).toString();
                        _test.displayMsg(refgroup);
                    }

                    if (refList.getReferenceList().size() == 4)
                    {
                        _test.displayResultSuccess(null);
                    }
                    else
                    {
                        StringBuffer fMsg = new StringBuffer();
                        fMsg.append("List size = ");
                        fMsg.append(refList.getReferenceList().size());
                        fMsg.append(" , expected 4.");
                        _test.displayResultFailure(fMsg.toString());
                    }
                }
                else
                {
                    _test.displayResultFailure("Wrong list.");
                }
            }
            catch (ServiceException srvex)
            {
                _test.displayResultFailure(srvex.toString());
            }
        }
        catch (Exception ex)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("Exception : ");
            msg.append(TestUtils.NEXT);
            msg.append(ex.toString());
            _test.displayResultError(msg.toString());
        }

        _test.displayFooterMethod("testGetReferenceListRequest");
    }

    /**
     * Tests the Reference list with Session scope.
     */
    public void testGetReferenceListSession()
    {
        _test.displayHeaderMethod("testGetReferenceListSession");

        try
        {
            try
            {
                ReferenceList refList = ReferenceService.getReferenceList("groups_sess",
                        null);
                _test.displayResultFailure("Scope session without userContext");

                return;
            }
            catch (Exception exp)
            {
            }

            try
            {
                ReferenceList refList = ReferenceService.getReferenceList("obj_sess",
                        _USERCONTEXT);

                if (refList.getName().equals("obj_sess"))
                {
                    _test.displayMsg("Values list :");

                    for (int i = 0; i < refList.getReferenceList().size();
                            i++)
                    {
                        String refgroup = ((ReferenceElement) refList.getReferenceList()
                                                                     .get(i)).toString();
                        _test.displayMsg(refgroup);
                    }

                    if (refList.getReferenceList().size() == 4)
                    {
                        _test.displayResultSuccess(null);
                    }
                    else
                    {
                        StringBuffer fMsg = new StringBuffer();
                        fMsg.append("List size = ");
                        fMsg.append(refList.getReferenceList().size());
                        fMsg.append(" , expected 4.");
                        _test.displayResultFailure(fMsg.toString());
                    }
                }
                else
                {
                    _test.displayResultFailure("Wrong list.");
                }
            }
            catch (ServiceException srvex)
            {
                _test.displayResultFailure(srvex.toString());
            }
        }
        catch (Exception ex)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("Exception : ");
            msg.append(TestUtils.NEXT);
            msg.append(ex.toString());
            _test.displayResultError(msg.toString());
        }

        _test.displayFooterMethod("testGetReferenceListSession");
    }

    /**
     * Tests the Reference list with Application scope.
     */
    public void testGetSearchReferenceListAppli()
    {
        _test.displayHeaderMethod("testGetSearchReferenceListAppli");

        try
        {
            try
            {
                ReferenceList refList = ReferenceService.getReferenceList("groups_appli",
                        null);

                if (refList.getName().equals("groups_appli"))
                {
                    _test.displayMsg("Groups list :");

                    for (int i = 0; i < refList.getReferenceList().size();
                            i++)
                    {
                        String refgroup = ((ReferenceElement) refList.getReferenceList()
                                                                     .get(i)).toString();
                        _test.displayMsg(refgroup);
                    }

                    if (refList.getReferenceList().size() == 4)
                    {
                        // checks if the test is ok
                        int i = 0;
                        List refsList = refList.getReferenceList();

                        while ((i < (refList.getReferenceList().size() - 1)) &&
                                ((((ReferenceElement) refsList.get(i++)).getLabel()).compareTo(
                                    ((ReferenceElement) refsList.get(i)).getLabel()) < 0))
                        {
                        }

                        if (i == (refList.getReferenceList().size() - 1))
                        {
                            _test.displayResultSuccess(null);
                        }
                        else
                        {
                            _test.displayResultFailure(null);
                        }
                    }
                    else
                    {
                        StringBuffer fMsg = new StringBuffer();
                        fMsg.append("List size = ");
                        fMsg.append(refList.getReferenceList().size());
                        fMsg.append(" , expected 4.");
                        _test.displayResultFailure(fMsg.toString());
                    }
                }
            }
            catch (ServiceException srvex)
            {
                _test.displayResultFailure(srvex.toString());
            }
        }
        catch (Exception ex)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("Exception : ");
            msg.append(TestUtils.NEXT);
            msg.append(ex.toString());
            _test.displayResultError(msg.toString());
        }

        _test.displayFooterMethod("testGetSearchReferenceListAppli");
    }

    /**
     * Tests the Reference list with Application scope and no sort.
     */
    public void testGetSearchReferenceListAppliNoSort()
    {
        _test.displayHeaderMethod("testGetSearchReferenceListAppliNoSort");

        try
        {
            try
            {
                ReferenceList refList = ReferenceService.getReferenceList("groups_appli_nt",
                        null);

                if (refList.getName().equals("groups_appli_nt"))
                {
                    _test.displayMsg("Groups list :");

                    for (int i = 0; i < refList.getReferenceList().size();
                            i++)
                    {
                        String refgroup = ((ReferenceElement) refList.getReferenceList()
                                                                     .get(i)).toString();
                        _test.displayMsg(refgroup);
                    }

                    if (refList.getReferenceList().size() == 4)
                    {
                        _test.displayResultSuccess(null);
                    }
                    else
                    {
                        StringBuffer fMsg = new StringBuffer();
                        fMsg.append("List size = ");
                        fMsg.append(refList.getReferenceList().size());
                        fMsg.append(" , expected 4.");
                        _test.displayResultFailure(fMsg.toString());
                    }
                }
            }
            catch (ServiceException srvex)
            {
                _test.displayResultFailure(srvex.toString());
            }
        }
        catch (Exception ex)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("Exception : ");
            msg.append(TestUtils.NEXT);
            msg.append(ex.toString());
            _test.displayResultError(msg.toString());
        }

        _test.displayFooterMethod("testGetSearchReferenceListAppliNoSort");
    }

    /**
     * Tests the Reference list with Request scope.
     */
    public void testGetSearchReferenceListRequest()
    {
        _test.displayHeaderMethod("testGetSearchReferenceListRequest");

        try
        {
            try
            {
                ReferenceList refList = ReferenceService.getReferenceList("groups_req",
                        null);

                if (refList.getName().equals("groups_req"))
                {
                    _test.displayMsg("Groups list :");

                    for (int i = 0; i < refList.getReferenceList().size();
                            i++)
                    {
                        String refgroup = ((ReferenceElement) refList.getReferenceList()
                                                                     .get(i)).toString();
                        _test.displayMsg(refgroup);
                    }

                    if (refList.getReferenceList().size() == 2)
                    {
                        _test.displayResultSuccess(null);
                    }
                    else
                    {
                        StringBuffer fMsg = new StringBuffer();
                        fMsg.append("List size = ");
                        fMsg.append(refList.getReferenceList().size());
                        fMsg.append(" , expected 2.");
                        _test.displayResultFailure(fMsg.toString());
                    }
                }
            }
            catch (ServiceException srvex)
            {
                _test.displayResultFailure(srvex.toString());
            }
        }
        catch (Exception ex)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("Exception : ");
            msg.append(TestUtils.NEXT);
            msg.append(ex.toString());
            _test.displayResultError(msg.toString());
        }

        _test.displayFooterMethod("testGetSearchReferenceListRequest");
    }

    /**
     * Tests the Reference list with Session scope.
     */
    public void testGetSearchReferenceListSession()
    {
        _test.displayHeaderMethod("testGetSearchReferenceListSession");

        try
        {
            try
            {
                try
                {
                    ReferenceList refList = ReferenceService.getReferenceList("groups_sess",
                            null);
                    _test.displayResultFailure(
                        "Scope session without userContext");

                    return;
                }
                catch (Exception exp)
                {
                }

                ReferenceList refList = ReferenceService.getReferenceList("groups_sess",
                        _USERCONTEXT);

                if (refList.getName().equals("groups_sess"))
                {
                    _test.displayMsg("Groups list :");

                    for (int i = 0; i < refList.getReferenceList().size();
                            i++)
                    {
                        String refgroup = ((ReferenceElement) refList.getReferenceList()
                                                                     .get(i)).toString();
                        _test.displayMsg(refgroup);
                    }

                    if (refList.getReferenceList().size() == 4)
                    {
                        _test.displayResultSuccess(null);
                    }
                    else
                    {
                        StringBuffer fMsg = new StringBuffer();
                        fMsg.append("List size = ");
                        fMsg.append(refList.getReferenceList().size());
                        fMsg.append(" , expected 4.");
                        _test.displayResultFailure(fMsg.toString());
                    }
                }
            }
            catch (ServiceException srvex)
            {
                _test.displayResultFailure(srvex.toString());
            }
        }
        catch (Exception ex)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("Exception : ");
            msg.append(TestUtils.NEXT);
            msg.append(ex.toString());
            _test.displayResultError(msg.toString());
        }

        _test.displayFooterMethod("testGetSearchReferenceListSession");
    }
}
